/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDConfiguracao.java
 *
 * Created on 24/12/2011, 12:58:37
 */
package view.admin;

import control.PostMasterControl;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.EntidadeSimpleFactory;
import model.PostMaster;

/**
 *
 * @author Felipe
 */
public class JDConfiguracao extends javax.swing.JDialog {

    private PostMasterControl controller;
    
    /** Creates new form JDConfiguracao */
    public JDConfiguracao(Frame parent, boolean modal) {
        super(parent, modal);
        controller = new PostMasterControl();
        initComponents();
        preencherForm();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        pnlGeral = new javax.swing.JPanel();
        pnlGeralRodape = new javax.swing.JPanel();
        btnCancelar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        pnlGeralForm = new javax.swing.JPanel();
        lblInfo1 = new javax.swing.JLabel();
        lblUsuario = new javax.swing.JLabel();
        lblSenha = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        lblConta = new javax.swing.JLabel();
        txtConta = new javax.swing.JTextField();
        lblSmtp = new javax.swing.JLabel();
        txtSmtp = new javax.swing.JTextField();
        ptxtSenha = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configurações");

        pnlGeral.setLayout(new java.awt.BorderLayout());

        pnlGeralRodape.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        pnlGeralRodape.setPreferredSize(new java.awt.Dimension(573, 54));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlGeralRodapeLayout = new javax.swing.GroupLayout(pnlGeralRodape);
        pnlGeralRodape.setLayout(pnlGeralRodapeLayout);
        pnlGeralRodapeLayout.setHorizontalGroup(
            pnlGeralRodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlGeralRodapeLayout.createSequentialGroup()
                .addContainerGap(239, Short.MAX_VALUE)
                .addComponent(btnSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        pnlGeralRodapeLayout.setVerticalGroup(
            pnlGeralRodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlGeralRodapeLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlGeralRodapeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnSalvar))
                .addContainerGap())
        );

        pnlGeral.add(pnlGeralRodape, java.awt.BorderLayout.PAGE_END);

        lblInfo1.setText("Configuração do postmaster");

        lblUsuario.setText("Usuário");

        lblSenha.setText("Senha");

        txtUsuario.setToolTipText("Usuario da conta de email");
        txtUsuario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtUsuarioFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtUsuarioFocusLost(evt);
            }
        });
        txtUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtUsuarioKeyPressed(evt);
            }
        });

        lblConta.setText("Conta");

        txtConta.setToolTipText("O endereço completo do email");
        txtConta.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtContaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtContaFocusLost(evt);
            }
        });
        txtConta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtContaKeyPressed(evt);
            }
        });

        lblSmtp.setText("Servidor SMTP");

        txtSmtp.setToolTipText("url do servidor smtp(envio)");
        txtSmtp.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtSmtpFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtSmtpFocusLost(evt);
            }
        });
        txtSmtp.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtSmtpKeyPressed(evt);
            }
        });

        ptxtSenha.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                ptxtSenhaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                ptxtSenhaFocusLost(evt);
            }
        });
        ptxtSenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                ptxtSenhaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout pnlGeralFormLayout = new javax.swing.GroupLayout(pnlGeralForm);
        pnlGeralForm.setLayout(pnlGeralFormLayout);
        pnlGeralFormLayout.setHorizontalGroup(
            pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlGeralFormLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlGeralFormLayout.createSequentialGroup()
                        .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblUsuario)
                            .addComponent(lblSenha)
                            .addComponent(lblConta)
                            .addComponent(lblSmtp))
                        .addGap(18, 18, 18)
                        .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(ptxtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                                .addComponent(txtConta, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                                .addComponent(txtSmtp, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))))
                    .addGroup(pnlGeralFormLayout.createSequentialGroup()
                        .addComponent(lblInfo1)
                        .addGap(187, 187, 187)))
                .addGap(548, 548, 548))
        );
        pnlGeralFormLayout.setVerticalGroup(
            pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlGeralFormLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(lblInfo1)
                .addGap(33, 33, 33)
                .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsuario)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSenha)
                    .addComponent(ptxtSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblConta)
                    .addComponent(txtConta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlGeralFormLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSmtp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblSmtp))
                .addContainerGap(72, Short.MAX_VALUE))
        );

        pnlGeral.add(pnlGeralForm, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Geral", pnlGeral);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-492)/2, (screenSize.height-361)/2, 492, 361);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtUsuarioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsuarioFocusGained
        txtUsuario.setBackground(new Color(255,255,220));
    }//GEN-LAST:event_txtUsuarioFocusGained

    private void txtUsuarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtUsuarioFocusLost
        txtUsuario.setBackground(new Color(255,255,255));
    }//GEN-LAST:event_txtUsuarioFocusLost

    private void ptxtSenhaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ptxtSenhaFocusGained
        ptxtSenha.setBackground(new Color(255,255,220));
    }//GEN-LAST:event_ptxtSenhaFocusGained

    private void ptxtSenhaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_ptxtSenhaFocusLost
        ptxtSenha.setBackground(new Color(255,255,255));
    }//GEN-LAST:event_ptxtSenhaFocusLost

    private void txtContaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtContaFocusGained
        txtConta.setBackground(new Color(255,255,220));
    }//GEN-LAST:event_txtContaFocusGained

    private void txtContaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtContaFocusLost
        txtConta.setBackground(new Color(255,255,255));
    }//GEN-LAST:event_txtContaFocusLost

    private void txtSmtpFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSmtpFocusGained
        txtSmtp.setBackground(new Color(255,255,220));
    }//GEN-LAST:event_txtSmtpFocusGained

    private void txtSmtpFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtSmtpFocusLost
        txtSmtp.setBackground(new Color(255,255,255));
    }//GEN-LAST:event_txtSmtpFocusLost

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        persistir();
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void txtUsuarioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUsuarioKeyPressed
         switch (evt.getKeyCode()) {
            case KeyEvent.VK_ESCAPE : this.dispose();
                                      break;
        }
    }//GEN-LAST:event_txtUsuarioKeyPressed

    private void ptxtSenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ptxtSenhaKeyPressed
         switch (evt.getKeyCode()) {
            case KeyEvent.VK_ESCAPE : this.dispose();
                                      break;
        }
    }//GEN-LAST:event_ptxtSenhaKeyPressed

    private void txtContaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtContaKeyPressed
         switch (evt.getKeyCode()) {
            case KeyEvent.VK_ESCAPE : this.dispose();
                                      break;
        }
    }//GEN-LAST:event_txtContaKeyPressed

    private void txtSmtpKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSmtpKeyPressed
         switch (evt.getKeyCode()) {
            case KeyEvent.VK_ENTER  : persistir();
            case KeyEvent.VK_ESCAPE : this.dispose();
                                      break;
        }
    }//GEN-LAST:event_txtSmtpKeyPressed

    private void preencherForm() {
        try {
            PostMaster ejb = controller.getPostMasterEJB();
            txtUsuario.setText(ejb.getUsuario());
            ptxtSenha.setText(ejb.getSenha());
            txtConta.setText(ejb.getConta());
            txtSmtp.setText(ejb.getSmtp());        
        } catch (Exception ex) {
            Logger.getLogger(JDConfiguracao.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Houve um erro ao tentar recuperar o registro da base de dados!\n" +
                                ex.getMessage());
        }        
    }
    
    private void persistir() {
        PostMaster ejb = (PostMaster) EntidadeSimpleFactory.getEntity(PostMaster.class);
        ejb.setUsuario(txtUsuario.getText());
        ejb.setSenha(ptxtSenha.getText());
        ejb.setConta(txtConta.getText());
        ejb.setSmtp(txtSmtp.getText());
        try {
            controller.setPostMasterEJB(ejb);
            this.dispose();
        } catch (Exception ex) {
            Logger.getLogger(JDConfiguracao.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("Houve um erro ao tentar salvar o registro na base de dados!\n" +
                                ex.getMessage());
        }
    
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lblConta;
    private javax.swing.JLabel lblInfo1;
    private javax.swing.JLabel lblSenha;
    private javax.swing.JLabel lblSmtp;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPanel pnlGeral;
    private javax.swing.JPanel pnlGeralForm;
    private javax.swing.JPanel pnlGeralRodape;
    private javax.swing.JPasswordField ptxtSenha;
    private javax.swing.JTextField txtConta;
    private javax.swing.JTextField txtSmtp;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
